<?php
class industry {

	var $CI;
	var $industry_model;

	public function __construct() {
		$this->CI =& get_instance();
		$this->CI->load->model("sys/sys_industry_model");
		$this->industry_model =$this->CI->sys_industry_model;
	}

	public function get_industrys($parent_id) {
		return $this->industry_model->query_industrys($parent_id);
	}
	
	public function  query_user_top_industrys($user_id){
		return $this->industry_model->query_user_top_industrys($user_id);
	}
	public function get_parent_id($industry_id){
		$industry= $this->industry_model->query_parent_id($industry_id);
		if ($industry) {
			return $industry['parent_id'];
		}else{
			return -1;
		}
	}

	public function save_industry($industry){
		$check_result=$this->check_industry($industry);
		if (!$check_result['result']) {
			return $check_result;
		}
		$industry_id =$this->industry_model->insert_industry($industry);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/sys_industry_manage";
		return $result;
	}

	public function check_industry($industry,$industry_id=""){
		$result['result'] =true;
		$result['message']="区域名称可以使用";
		if ($industry['industry_name']=="") {
			$result['result']=false;
			$result['message']="区域名称不能为空";
			return $result;
		}
		$industry_info =$this->get_industry_info_by_name($industry['industry_name']);
		if ($industry_info=="") {
			$result['result']=true;
			$result['message']="区域名称可以使用";
			return $result;
		}
		if ($industry['parent_id']==$industry_info['parent_id']&&$industry_info['industry_id']!=$industry_id) {
			$result['result']=false;
			$result['message']="相同区域已经存在,请重新输入";
			return $result;
		}
		return $result;
	}

	public function get_industry_info($industry_id){
		return $this->industry_model->query_industry_info($industry_id);
	}

	public function get_industry_info_by_name($industry_name){
		return $this->industry_model->query_industry_info_by_name($industry_name);
	}

	public function set_delete_industry($industry_id){
		$this->industry_model->set_delete_industry($industry_id);

	}
	public function update_industry($industry,$industry_id){
		$check_result=$this->check_industry($industry,$industry_id);
		if (!$check_result['result']) {
			return $check_result;
		}
		$industry_id =$this->industry_model->update_industry($industry,$industry_id);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/sys_industry_manage";
		return $result;
	}
}
?>